
const path = require("path");
const fs = require("fs");
const chalk = require("chalk");
const express = require("express");
let config= {};

const delayExit=()=>{
    console.log(`3s后自动退出`); 
    setTimeout(()=>{
    },3000)
};

const startHttpServer = () => {
  const app = express();
  logMiddleware=function(req,res,next){
    console.log(req.headers);
    console.log(req.url);
    next();
  };
  app.use(logMiddleware);

  // app.get("/authorizeNginxUser", function (req, res) {
  //   res.statusCode=200;
  //   // res.send(JSON.stringify(config.users));
  //   res.send("验证通过")
  // });
  // app.get("/data", function (req, res) {
  //   res.send("测试数据");
  // });
  // app.get("/refuse", function (req, res) {
  //   res.statusCode=401;
  //   res.send("拒绝接口");
  // });
  // app.get("/*", function (req, res) {
  //   res.statusCode=200;
  //   res.send("测试接口");
  // });

  app.post("/api/zhilian/GenerateToken/", function (req, res) {
    console.log("req...............",req.headers);
    res.statusCode=200;
    res.send("post测试接口");
  });

  app.post("/*", function (req, res) {
    console.log("req...............",req.headers);
    res.statusCode=200;
    res.send("post测试接口");
  });
  app.listen(config.port, (err) => {
    if (err) {
      console.log(chalk.red(`http-server启动失败:【${err}】`));
      delayExit();
      return;
    }
    console.log(chalk.green(`服务已在端口:【${config.port}】启动`));
  });
};

fs.readFile(path.resolve(process.cwd(), "./config.json"), (err, data) => {
  if (err) {
    console.log(chalk.red(`读取配置失败:【${err}】`));
    delayExit();
    return;
  }
  try{
    config=JSON.parse(data.toString());
    console.log("读取配置成功")
  }catch(err){
    console.log(chalk.red(`配置文件格式不正确:【${err}】`));
    delayExit();
    return;
  }
  startHttpServer();
});
